Type TilesetWindow Extends ToolWindow


	Field TileAsset : Tileset 
	
	Method New()
	
		ToolType = TOOL_TYPE_TILE
	
	End Method 
	

	Method Update(EID:Int,Obj:Object,EEX : Object = Null)
	
	
		Select EID
		
			Case EVENT_WINDOWCLOSE
				If Obj = Window Then							
					FreeGadget Window
					ListRemove(ParentList,Self)	
				EndIf	
		
			Case EVENT_GADGETACTION
				Select Obj
				
					Case Gadgets[1]
						
						'browse for file
						Local filter : String ="Image Files:png,jpg,bmp"
						Local filename : String = RequestFile( "Select graphic file to open",filter )
						
						If filename <> "" Then
						
							If TileAsset = Null Then TileAsset = New Tileset
							
							'TileAsset = TileAsset
							
							TileAsset.FileName = filename
							TileAsset.ShortName = Replace(filename,Replace(ExtractDir(filename),"/","\"),"")
							TileAsset.FileBank = LoadBank(TileAsset.FileName)
							
							SetGadgetText(Gadgets[1],TileAsset.ShortName)
						EndIf
						
					
					Case Gadgets[11]
					
						If TileAsset.FileBank = Null Then 
							Notify "You do not have an Image selected!"
						Else
						
						
							TileAsset.TilesetFromBank(TileAsset.FileBank,Int(TextFieldText(Gadgets[8])),Int(TextFieldText(Gadgets[4])), Int(TextFieldText(Gadgets[6])), Int(GadgetExtra(AssetNode).ToString())|DYNAMICIMAGE)
							TileAsset.AssetParent = Asset(GadgetExtra(AssetNode))
							AddTreeViewNode( TileAsset.ShortName,AssetNode,1,TileAsset )
							DebugLog "Path: " + GadgetText(AssetNode)+TileAsset.ShortName
							If TileAsset.AssetPath = "" Then
								DebugLog "new Path: "+TileAsset.FetchAssetPath()+TileAsset.ShortName
								
								TileAsset.AssetPath = TileAsset.FetchAssetPath()+TileAsset.ShortName
							EndIf
							SaveBank(TileAsset.FileBank,"Projects\"+CurrentProject+"\"+TileAsset.AssetPath)
							TileAsset.Save()
							For Local Gad : TGadget = EachIn Gadgets
							
								If Gad <> Null Then
									FreeGadget Gad
									'ListRemove(Gadgets,Gad)
								EndIf
							Next
							FreeGadget Window
							ListRemove(ParentList,Self)	
							Gadgets = Null					
						EndIf
					
					Case Gadgets[12]
						FreeGadget Window
						ListRemove(ParentList,Self)									
				
				End Select
		
		
		End Select
	
	
	
		Super.Update(EID,Obj)
	
	End Method
	
	
	Method Set()
		Local Gads : TGadget[13]
		If TileAsset = Null Then TileAsset = New Tileset
		Window = CreateWindow( Title,Pos.x,Pos.y,Pos.w,Pos.h,Null,WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CENTER )
		Gads[0] = CreatePanel(0,0,ClientWidth(Window),80,Window,PANEL_GROUP,"Image File")'CreateLabel("Image File",10,10,100,20,Window)
		Gads[1] = CreateButton(TileAsset.Shortname,10,10,ClientWidth(Gads[0])-20,20,Gads[0])
		If EditMode = True Then
			DisableGadget(Gads[1])
		EndIf
		Gads[2] = CreatePanel(0,80,ClientWidth(Window),120,Window,PANEL_GROUP,"Properties")
		Gads[3] = CreateLabel("Cell W:",10,10,40,20,Gads[2])
		Gads[4] = CreateTextField(50,10,50,20,Gads[2])
		SetGadgetText(Gads[4],TileAsset.Cell_W)
		Gads[5] = CreateLabel("Cell H:",10,30,40,20,Gads[2])
		Gads[6] = CreateTextField(50,30,50,20,Gads[2])
		SetGadgetText(Gads[6],TileAsset.Cell_H)
		Gads[7] = CreateLabel("Frames:",110,10,40,20,Gads[2])
		Gads[8] = CreateTextField(150,10,50,20,Gads[2])
		SetGadgetText(Gads[8],TileAsset.Frames)
		Gads[9] = CreateLabel("Flag:",110,30,40,20,Gads[2])
		Gads[10] = CreateComboBox(150,30,100,20,Gads[2])
		
		AddGadgetItem(Gads[10],"Masked",0,-1,"The image is masked with the current mask color",""+MASKEDIMAGE)
		AddGadgetItem(Gads[10],"Filtered",0,-1,"The image is smoothed when scaled up to greater than its original size, when rotated, or when drawn at fractional pixel coordinates. ",""+FILTEREDIMAGE)
		AddGadgetItem(Gads[10],"MipMap",0,-1,"The image is smoothed when scaled down to less than its original size. ",""+MIPMAPPEDIMAGE)
		Select TileAsset.Flags
		
			Case MASKEDIMAGE
				SelectGadgetItem(Gads[10],0)
		
			Case FILTEREDIMAGE
				SelectGadgetItem(Gads[10],1)

			Case MIPMAPPEDIMAGE
				SelectGadgetItem(Gads[10],2)
		
			Default 
				SelectGadgetItem(Gads[10],0)
		
		End Select			
		
		
		If EditMode = True Then
			Gads[11] = CreateButton("Save Tileset",10,220,100,20,Window)
		Else
			Gads[11] = CreateButton("Create Tileset",10,220,100,20,Window)
		EndIf
		Gads[12] = CreateButton("Cancel",120,220,100,20,Window)
		
		Gadgets = Gads
	End Method
	
	
	
	
End Type
